//
//  GQTabBarController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/7.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQTabBarController.h"
#import "GQTabBar.h"
#import "GQHomeController.h"
#import "GQDiscoverController.h"
#import "GQMessageController.h"
#import "GQProfileController.h"
#import "GQMainNavigationController.h"
#import "GQCoordinate.h"
#import "UIView+ScreenShot.h"
#import "GQDistributeStatusController.h"
#import "GQMainNavigationController.h"
#import "GQDistributePhotoController.h"


@interface GQTabBarController () <GQTabBarDelegate>

@property (strong, nonatomic) NSMutableArray *items;

@property (assign, getter=isOpen, nonatomic) BOOL open;
@property (assign, getter=isAnimating, nonatomic) BOOL animating;
@property (assign, getter=isOnce) BOOL once;

@property (strong, nonatomic) UIButton *shareButton;
@property (strong, nonatomic) NSMutableArray *btns;
@property (strong, nonatomic) NSMutableDictionary *coordianteDict;
@property (strong, nonatomic) NSArray *imageArr;
@property (strong, nonatomic) UIView *coverView;
@property (strong, nonatomic) UIView *leftCoverView;
@property (strong, nonatomic) UIView *rightCoverView;
//@property (weak, nonatomic) GQTabBar *tBar;
@end

@implementation GQTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpAllChildController];
    
    UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureClick1)];
    [self.coverView addGestureRecognizer:tapGestureRecognizer1];
    [self setUpTabBar];
    UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureClick2)];
    UITapGestureRecognizer *tapGestureRecognizer3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureClick3)];

    [self.leftCoverView addGestureRecognizer:tapGestureRecognizer2];
    [self.rightCoverView addGestureRecognizer:tapGestureRecognizer3];
    [self setUpShareButtons];
    
}

- (void)tapGestureClick1
{
    [self closeMenu:_shareButton];
}

- (void)tapGestureClick2
{
    [self closeMenu:_shareButton];
}

- (void)tapGestureClick3
{
    [self closeMenu:_shareButton];
}

- (void)setUpShareButtons
{
    int i = 0;
    for (NSString *imageName in self.imageArr) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"distribute_character"] forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.alpha = 0;
        UIImage *image = [UIImage imageNamed:imageName];
        btn.frame = CGRectMake(0, 0, 50, 50);
        [btn setImage:image forState:UIControlStateNormal];
        btn.center = CGPointMake(self.view.width / 2, self.view.height - 49 / 2);
        i++;
        [self.btns addObject:btn];
        [self.view addSubview:btn];
    }
}

- (void)btnClick:(UIButton *)btn
{
    
    [self closeMenu:_shareButton];
    
    if (btn.tag == 2) {
        GQDistributeStatusController *GQDSC = [[GQDistributeStatusController alloc] init];
        GQDSC.view.backgroundColor = [UIColor whiteColor];
        GQMainNavigationController *GQMNC = [[GQMainNavigationController alloc] initWithRootViewController:GQDSC];
        [self presentViewController:GQMNC animated:YES completion:nil];
    }
    if (btn.tag == 1) {
        GQDistributePhotoController *GQDPC = [[GQDistributePhotoController alloc] init];
        GQDPC.view.backgroundColor = [UIColor whiteColor];
        GQMainNavigationController *GQMNC = [[GQMainNavigationController alloc] initWithRootViewController:GQDPC];
        [self presentViewController:GQMNC animated:YES completion:nil];
    }
    if (btn.tag == 0) {
        GQDistributeStatusController *GQDSC = [[GQDistributeStatusController alloc] init];
        GQDSC.type = @"vote";
        GQDSC.view.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1.0];
        GQMainNavigationController *GQMNC = [[GQMainNavigationController alloc] initWithRootViewController:GQDSC];
        [self presentViewController:GQMNC animated:YES completion:nil];
    }
    
}

- (void)setUpTabBar
{
    GQTabBar *GQTB = [GQTabBar sharedGQTabBar];
    GQTB.frame = self.tabBar.frame;
    GQTB.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
    GQTB.items = self.items;
    GQTB.delegate = self;
    [self.view addSubview:GQTB];
    [self.tabBar removeFromSuperview];
}

- (void)setUpAllChildController
{
    GQHomeController *GQHC = [[GQHomeController alloc] init];
    [self setUpOneChildViewController:GQHC image:[UIImage imageNamed:@"tool_home"] selectedImage:[UIImage imageNamed:@"tool_home_selected"] title:@"主页"];
    
    GQDiscoverController *GQDC = [[GQDiscoverController alloc] init];
    [self setUpOneChildViewController:GQDC image:[UIImage imageNamed:@"tool_discover"] selectedImage:[UIImage imageNamed:@"tool_discover_selected"] title:@"发现"];

    GQMessageController *GQMC = [[GQMessageController alloc] init];
    [self setUpOneChildViewController:GQMC image:[UIImage imageNamed:@"tool_message"] selectedImage:[UIImage imageNamed:@"tool_message_selected"] title:@"消息"];

    GQProfileController *GQPC = [[GQProfileController alloc] init];
    [self setUpOneChildViewController:GQPC image:[UIImage imageNamed:@"tool_profile"] selectedImage:[UIImage imageNamed:@"tool_profile_selected"] title:@"我"];
}

- (void)setUpOneChildViewController:(UIViewController *)VC image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title
{
    VC.title = title;
    VC.tabBarItem.image = image;
    VC.tabBarItem.selectedImage = selectedImage;
    [self.items addObject:VC.tabBarItem];
    
    GQMainNavigationController *GQMNC = [[GQMainNavigationController alloc] initWithRootViewController:VC];
    GQMNC.navigationBar.barTintColor = kNavigationBarColor;
    [self addChildViewController:GQMNC];
}

#pragma mark - 当点击按钮的时候调用
- (void)tabBar:(GQTabBar *)tabBar didClickButton:(NSInteger)index
{
    if (index == 0 && self.selectedIndex == index) {
    
    }
    self.selectedIndex = index;
}

- (void)tabBar:(GQTabBar *)tabBar didClickShareButton:(UIButton *)button
{
    self.shareButton = button;
    if (_animating) {
        return;
    }
    if (_open) {
        [self closeMenu:button];
    }else{
        [self openMenu:button];
    }
}

- (void)calculatePopMenuCoordinate
{
    NSInteger count = self.btns.count;
    float degrees = 180 / 4;
    float radius = (self.view.width - 20) / 2;
    float centerX = kScreenWidth / 2;
    float centerY = kScreenHeight + (kToolBarHeight - 40) * 0.5;
    for (int i = 0; i < count; i++) {
        float radian = (degrees * (i + 1) * M_PI / 180);
        float cosRadian = cosf(radian);
        float sinRadian = sinf(radian);
        float radiusRadianX = radius * cosRadian;
        float radiusRadianY = radius *sinRadian;
        GQCoordinate *coordinate = [[GQCoordinate alloc] init];
        coordinate.x = [NSNumber numberWithFloat:centerX + radiusRadianX];
        coordinate.y = [NSNumber numberWithFloat:centerY - radiusRadianY];
        [self.coordianteDict setObject:coordinate forKey:[NSNumber numberWithInt:i]];
    }
}


- (void)closeMenu:(UIButton *)button
{
    
    _animating = YES;
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
        NSInteger count = _btns.count;
        for (int i = 0; i < count; i++) {
            UIButton *btn = self.btns[i];
            btn.alpha = 0;
            btn.center = CGPointMake(self.view.width / 2, self.view.height - 49 / 2);
        }
        float rads = - 45 * M_PI / 180;
        button.layer.transform = CATransform3DMakeRotation(rads, 0, 0, 0);
    } completion:^(BOOL finished) {
        _open = NO;
        _animating = NO;
    }];
    self.coverView.alpha = 0;
    self.leftCoverView.alpha = 0;
    self.rightCoverView.alpha = 0;
}

- (void)openMenu:(UIButton *)button
{
    
    self.coverView.alpha = 0.5;
    self.coverView.backgroundColor = [UIColor blackColor];
    self.leftCoverView.alpha = 1.0;
    self.rightCoverView.alpha = 1.0;
    [self.view bringSubviewToFront:button];
    [self calculatePopMenuCoordinate];
    _animating = YES;
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
        NSInteger count = self.btns.count;
        for (int i = 0; i < count; i++) {
            UIButton *btn = self.btns[i];
            btn.alpha = 1.0;
            GQCoordinate *coordinate = [_coordianteDict objectForKey:[NSNumber numberWithInt:i]];
            btn.center = CGPointMake([coordinate.x floatValue], [coordinate.y floatValue]);
            
        }
        float rads = 45 * M_PI / 180;
        button.layer.transform = CATransform3DMakeRotation(rads, 0, 0, 1);
    
    } completion:^(BOOL finished) {
        _open = YES;
        _animating = NO;
        
    }];

}

#pragma mark - 懒加载

- (NSMutableArray *)btns
{
    if (_btns == nil) {
        _btns = [NSMutableArray array];
        
    }
    return _btns;
}

- (NSMutableDictionary *)coordianteDict
{
    if (_coordianteDict == nil) {
        _coordianteDict = [NSMutableDictionary dictionary];
    }
    return _coordianteDict;
}

- (NSMutableArray *)items
{
    if (_items == nil) {
        _items = [NSMutableArray array];
    }
    return _items;
}

- (NSArray *)imageArr
{
    if (_imageArr == nil) {
        _imageArr = [NSArray arrayWithObjects:@"distribute_vote", @"distribute_image", @"distribute_character", nil];
    }
    return _imageArr;
}

- (UIView *)coverView
{
    if (_coverView == nil) {
        _coverView = [[UIView alloc] initWithFrame:self.view.bounds];
        _coverView.alpha = 0;
        [self.view addSubview:_coverView];
       
    }
    return _coverView;
}

- (UIView *)leftCoverView
{
    if (_leftCoverView == nil) {
        _leftCoverView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kToolBarHeight, kScreenWidth * 0.4, kToolBarHeight)];
        _leftCoverView.alpha = 0;
         [self.view addSubview:_leftCoverView];
    }
    return _leftCoverView;
}

- (UIView *)rightCoverView
{
    if (_rightCoverView == nil) {
        _rightCoverView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth * 0.6, kScreenHeight - kToolBarHeight, kScreenWidth * 0.4, kToolBarHeight)];
        _rightCoverView.alpha = 0;
        [self.view addSubview:_rightCoverView];
    }
    return _rightCoverView;
}

@end
